﻿using MediatR;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.BuildingD
{
    public class BuildingTypeSeachHandler : IRequestHandler<BuildingTypeSeachCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 栋舍类型仓储
        /// </summary>
        public readonly IBaseRepository<BuildingType> BuildingTypeRepository;
        public readonly ILogger<BuildingTypeSeachHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="buildingTypeRepository">栋舍类型</param>
        /// <param name="logger">日志</param>
        public BuildingTypeSeachHandler(IBaseRepository<BuildingType> buildingTypeRepository, ILogger<BuildingTypeSeachHandler> logger)
        {
            BuildingTypeRepository = buildingTypeRepository;
            this.logger = logger;
        }
        #endregion

        #region 下拉框 栋舍类型
        /// <summary>
        /// 下拉框 栋舍类型
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(BuildingTypeSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("下拉框栋舍类型显示成功!");
                var res = BuildingTypeRepository.GetAll().ToList();
                result.Code = (int)CommonEnumCode.success;
                result.Data = res;
                result.Msg = "下拉框:栋舍类型";
            }
            catch (Exception ex)
            {
                logger.LogError("下拉框栋舍类型显示失败!" + ex.Message);
                result.Msg = "下拉框:栋舍类型失败";
            }
            return result;
        }
        #endregion

    }
}
